﻿using System.Text.RegularExpressions;

namespace AppConfigVerifier
{
	public class IncludeExcludeRule : Rule
	{
		public IncludeExcludeRule(Regex regex, Location ruleLocation, bool include)
		{
			this.regex = regex;
			this.ruleLocation = ruleLocation;
			this.include = include;
		}

		readonly bool include;
		readonly Regex regex;
		readonly Location ruleLocation;

		public override void Apply(LocalContext context)
		{
			if (context.Token.Location < ruleLocation)
			{
				return;
			}
			if (regex == null)
			{
				if (context.Token.Location.Line == ruleLocation.Line + 1)
				{
					OnMatched(context.GetExtension<InclusionExclusionExtension>());
				}
			}
			else if (regex.IsMatch(context.TypeName))
			{
				OnMatched(context.GetExtension<InclusionExclusionExtension>());
			}
		}

		void OnMatched(InclusionExclusionExtension extension)
		{
			extension.IsIncluded = include;
		}
	}
}
